package com.wolfalpha.utron.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wolfalpha.utron.R;

import java.util.Vector;

/**
 * 自定义的对话框
 *
 * @author
 */
public class CallsAlertDialog implements View.OnClickListener {
    private Context context;
    private android.app.AlertDialog ad;
    private TextView titleView;
    private LinearLayout c1;
    private LinearLayout c2;
    private LinearLayout c3;
    private LinearLayout c4;
    private LinearLayout c5;
    private TextView sure;
    private TextView cancel;

    private LayoutInflater inflater;

    private Vector<Boolean> cs = new Vector<Boolean>();


    public CallsAlertDialog(Context context) {
        // TODO Auto-generated constructor stub
        this.context = context;
        inflater = LayoutInflater.from(context);
        ad = new android.app.AlertDialog.Builder(context).create();
        ad.show();
        //关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = ad.getWindow();
        window.setContentView(R.layout.dialog_calls);
        titleView = (TextView) window.findViewById(R.id.tv_dialog_base_title);
        c1 = (LinearLayout) window.findViewById(R.id.ll_dialog_calls_1);
        c2 = (LinearLayout) window.findViewById(R.id.ll_dialog_calls_2);
        c3 = (LinearLayout) window.findViewById(R.id.ll_dialog_calls_3);
        c4 = (LinearLayout) window.findViewById(R.id.ll_dialog_calls_4);
        c5 = (LinearLayout) window.findViewById(R.id.ll_dialog_calls_5);
        sure = (TextView) window.findViewById(R.id.tv_dialog_base_sure);
        cancel = (TextView) window.findViewById(R.id.tv_dialog_base_cancel);

        c1.setOnClickListener(this);
        c2.setOnClickListener(this);
        c3.setOnClickListener(this);
        c4.setOnClickListener(this);
        c5.setOnClickListener(this);

        setPositiveButton("确认");
        setNegativeButton("取消");

        cs.add(false);
        cs.add(false);
        cs.add(false);
        cs.add(false);
        cs.add(false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_dialog_calls_1:
                c1.setSelected(!cs.get(0));
                cs.setElementAt(!cs.get(0), 0);
                break;
            case R.id.ll_dialog_calls_2:
                c2.setSelected(!cs.get(1));
                cs.setElementAt(!cs.get(1), 1);
                break;
            case R.id.ll_dialog_calls_3:
                c3.setSelected(!cs.get(2));
                cs.setElementAt(!cs.get(2), 2);
                break;
            case R.id.ll_dialog_calls_4:
                c4.setSelected(!cs.get(3));
                cs.setElementAt(!cs.get(3), 3);
                break;
            case R.id.ll_dialog_calls_5:
                c5.setSelected(!cs.get(4));
                cs.setElementAt(!cs.get(4), 4);
                break;
        }
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        titleView.setText(title);
    }


    /**
     * 设置按钮
     *
     * @param text
     */
    public void setPositiveButton(String text) {
        sure.setText(text);
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    String result = "";
                    if (cs.get(0)) result += "技术;";
                    if (cs.get(1)) result += "设计;";
                    if (cs.get(2)) result += "运营;";
                    if (cs.get(3)) result += "营销;";
                    if (cs.get(4)) result += "产品;";
                    mListener.finish(result);
                }
                dismiss();
            }
        });
    }

    /**
     * 设置按钮
     *
     * @param text
     */
    public void setNegativeButton(String text) {
        cancel.setText(text);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    /**
     * 关闭对话框
     */
    public void dismiss() {
        ad.dismiss();
    }

    private OnFinishedListener mListener;

    public void setOnFinishedListener(OnFinishedListener listener) {
        mListener = listener;
    }


    public interface OnFinishedListener {
        public void finish(String input);
    }
}